package com.coollang.smartbra.presenter;

import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.fragment.FirstFragment;
import com.coollang.smartbra.model.BaseModel;
import com.coollang.smartbra.model.SportPlanModel;
import com.coollang.smartbra.model.TodayPlanDetail;
import com.coollang.smartbra.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.realm.Realm;

/**
 * @author: Timmy
 * @data: 2017/11/18
 * @Fuction:
 */
public class FirstFragmentPresenter extends BasePresenter<BaseModel> {

    private FirstFragment mFirstFragment;
    private HashMap<String, String> mRequestMap;
    public int mTodayPosition;

    public FirstFragmentPresenter(FirstFragment firstFragment) {
        super(BaseModel.class);
        mFirstFragment = firstFragment;
    }

    @Override
    public void setRequestInfo() {

    }

    public void getMyPlan() {
        mRequestMap = new HashMap<>(16);
        mRequestMap.put("username", MyApplication.mLogInDataBean.getUsername());
        setBaseOkgo1("plan/get-user-plan");
    }

    @Override
    protected Map<String, String> getData() {
        return mRequestMap;
    }

    @Override
    protected void parsrData(BaseModel baseModel) {

    }

    @Override
    protected void parsrData(String data) {
        SportPlanModel model = mGson.fromJson(data, SportPlanModel.class);
        if (model.getCode() == 200) {
            List<SportPlanModel.DataBean> dataBeen = model.getData();
            int size = dataBeen.size();
            for (int i = 0; i < size; i++) {
                SportPlanModel.DataBean dataBean = dataBeen.get(i);
                boolean today = TimeUtils.isToday(dataBean.getDate());
                if (today) {
                    mTodayPosition = i;
                    break;
                }
            }

            // 今日计划
            SportPlanModel.DataBean dataBean = dataBeen.get(mTodayPosition);
            /**
             * 存储到数据库当中
             */
            saveTodayActionDetail(dataBean);
            mFirstFragment.setTitle(mTodayPosition + 1);
        }
    }

    /**
     * 存储今日数据到数据库中
     */
    private void saveTodayActionDetail(final SportPlanModel.DataBean dataBean) {
        if (dataBean != null) {
            // 保存到数据库中
            mRealm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    // 今日计划
                    List<SportPlanModel.DataBean.ActionDataBean> actionData = dataBean.getActionData();

                    List<TodayPlanDetail> todayPlanDetails = new ArrayList<>();
                    for (SportPlanModel.DataBean.ActionDataBean actionDataBean : actionData) {
                        TodayPlanDetail planDetail = new TodayPlanDetail();
                        planDetail.setActionID(actionDataBean.getSportcode());
                        planDetail.setActionCount(actionDataBean.getCount());
                        planDetail.setFinishCount(actionDataBean.getFinish());
                        planDetail.setSportName(actionDataBean.getSportName());

                        todayPlanDetails.add(planDetail);
                    }
                    //保证存储的数据唯一
                    mRealm.copyToRealmOrUpdate(todayPlanDetails);
                }
            });
        }
    }
}
